<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>

import { formatTime } from '@/utils/index.js'
export default {
  name: 'echart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '2.5rem'
    },
    options: {
      type: Object,
      default: () => ({})
    },
    t: {
      type: String,
      default: '0'
    },
  },
  data() {
    return {
      chart: null,
      thisTT:null,
    }
  },
  watch: {
    options: {
      handler(options) {
        // 初始化echart 
        this.chart = this.$echarts.init(this.$el)
        this.chart.setOption(options, true)
        this.setAutoTip(options)
      },
      deep: true
    },
    t: {
      handler(t) {
        this.chart.resize();
      },
      deep: true
    },
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    this.destroyChart();
  },
  methods: {
    destroyChart() {
      if (this.chart) {
        this.chart.dispose()
        this.chart = null
      }
    },
    initChart() {
      if(this.thisTT){
        this.thisTT.clearLoop();
      }
      // 初始化echart 
      this.chart = this.$echarts.init(this.$el)
      this.chart.setOption(this.options, true)
      this.setAutoTip(this.options)
    },
    setAutoTip(options){
      const mykey = formatTime(new Date(), 'yyyyMMddHHmmssSSS')
      sessionStorage.setItem(this.id,mykey);
      this.thisTT =  this.$autoToolTip(this.chart, options, { loopSeries: true },this.id,mykey);
    }
  }
}
</script>